<?php
/**
 * @desc 运费管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_transportprice extends ctrl_base {
    private static $dirName = 'transportprice';
    
    /**
     * @desc 地区列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('tprice_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

		$tmp_id = isset($_GET['tmp_id']) ? intval($_GET['tmp_id']) : 0;
		$tmp_info = model_transporttmp::get_transporttmp_by_id($tmp_id);
    	//搜索条件组合
		$condition = array();
		if(!empty($tmp_id)) {
    		$condition['tmp_id'] = $tmp_id;
    	}
		//获取模板参数
		$transporttmp = model_transporttmp::get_transporttmp_by_id($tmp_id);
		//订单获取
		switch($tmp_info['tmp_type']){
			case 1:
				$area_list = model_transportprice::get_transportprice_motor($nextpage,0,$condition);
				if(!empty($area_list)){
					foreach($area_list as $key=>$value){
						if($area_list[$key]['street'] == ""){
							$area_list[$key]['street'] = "";
						}
					}
				}
			break;
			case 2:$area_list = model_transportprice::get_transportprice_train($nextpage,0,$condition);break;
			case 3:
				$area_list = model_transportprice::get_transportprice_ship($nextpage,0,$condition);
				if(!empty($area_list)){
					foreach($area_list as $key=>$value){
						if($area_list[$key]['port_dest'] == ""){
							$area_list[$key]['port_dest'] = "";
						}
					}
				}
			break;
			case 4:$area_list = model_transportprice::get_transportprice_smotor($nextpage,0,$condition);break;
		}
		$total = array_pop($area_list);//var_dump($area_list);exit;
		if(empty($area_list)){
			self::ajax_ouput(300, '操作失败,尚未设置模板地址');
		}
		$section = json_decode($transporttmp['section'], true);
		$count = count($section);
		//模板赋值
    	self::smarty() -> assign('area_list', $area_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('tmp_id', $tmp_id);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		self::smarty() -> assign('tmp_info', $tmp_info);
		self::smarty() -> assign('section', $section);
		self::smarty() -> assign('count', $count);
		self::smarty() -> display(self::$dirName . '/transportprice_list.html');
    	    	
    }
	//添加地区
	/*public static function transportprice_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('tprice_add'));
		$tmp_id = isset($_GET['tmp_id']) ? intval($_GET['tmp_id']) : 0;
		if($_POST){

			$transportprice = self::get_request_data();
			if($transportprice['min_weight'] >= $transportprice['max_weight']){
				self::ajax_ouput(300, '添加失败,价格区间写入有误');
			}
			$transportprice['transport_tmp_id'] = $tmp_id;
			$transportprice['author'] = $_SESSION['admin']['username'];
			$transportprice['editer'] = $_SESSION['admin']['username'];

			if(model_transportprice::transportprice_exists($tmp_id, $transportprice['area_id'], $transportprice['min_weight'])){
				self::ajax_ouput(300, '添加失败,该运费模板已存在');
			}
			$result = model_transportprice::transportprice_add($transportprice);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','transportprice','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		$tmp_info = model_transporttmp::get_transporttmp_by_id($tmp_id);
		self::smarty() -> assign('tmp_info', $tmp_info);
		$province = model_area::get_province_list();
		array_unshift($province,array("province"=>"请选择省份"));
		self::smarty() -> assign('province', $province);
		self::smarty() -> assign('tmp_id', $tmp_id);
		self::smarty() -> display(self::$dirName . '/transportprice_add.html');
	}
	//删除运费模板
	public static function transportprice_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('tprice_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_transportprice::update_transportprice_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','transportprice');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑区域
	public static function transportprice_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$transportprice = model_transportprice::get_transportprice_by_id($id);
		$ctrl_tag = 'tprice_edit_other';
		if($transportprice['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'tprice_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$tmp_id = $transportprice['transport_tmp_id'];
			$transportprice = array();

			$transportprice = self::get_request_data();
			$transportprice['editer'] = $_SESSION['admin']['username'];
			unset($transportprice['id']);
			if(model_transportprice::transportprice_exists($tmp_id, $transportprice['area_id'], $transportprice['min_weight'], $id)){
				self::ajax_ouput(300, '修改失败,该模板已存在');
			}
			$result = model_transportprice::update_transportprice_info(explode(',',$id), $transportprice);
	    	
		    if(!empty($result)){
				self::ajax_ouput(200, '操作成功','transportprice','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		$tmp_info = model_transporttmp::get_transporttmp_by_id($transportprice['transport_tmp_id']);
		self::smarty() -> assign('tmp_info', $tmp_info);
		switch($tmp_info['tmp_type']){
			case 1:self::motor_addr($transportprice['area_id']);break;
			case 2:self::train_addr($transportprice['area_id']);break;
			case 3:self::ship_addr($transportprice['area_id']);break;
			case 4:self::smotor_addr($transportprice['area_id']);break;
		}
    	self::smarty() -> assign('transportprice', $transportprice);
    	self::smarty() -> display(self::$dirName . '/transportprice_edit.html');
    }
	//获取最小值
	public static function get_min_weight(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$area_id = intval($_POST['area_id']);
		$tmp_id = intval($_POST['tmp_id']);
		$temp_info = model_transportprice::get_min_weight($area_id, $tmp_id);
		$return['error'] = 0;
		$return['val'] = empty($temp_info['max_weight'])?0:$temp_info['max_weight'];
		echo json_encode($return);exit;
	}
	//汽运地址处理
	private static function motor_addr($area_id){
		//地区信息
		$addr = model_area::get_tmp_motor_addr($area_id);
		self::smarty() -> assign('addr', $addr);
		//省份
		$province = model_area::get_province_list();
		array_unshift($province,array("province"=>"请选择省会"));
		self::smarty() -> assign('province', $province);
		//城市
		$city = model_area::get_city_list($addr['province']);
		array_unshift($city,array("area_id"=>"", 'city'=>'请选择城市'));
		self::smarty() -> assign('city', $city);
		//区县
		$area = model_area::get_areas_list($addr['area_id']);
		array_unshift($area,array("area_motor_id"=>"", 'area'=>'请选择区县'));
		self::smarty() -> assign('area', $area);
		//街道
		$street = model_area::get_street_list($area_id);
		array_unshift($street,array("area_motor_id"=>"", 'street'=>'请选择街道'));
		self::smarty() -> assign('street', $street);
	}
	//火运地址处理
	private static function train_addr($area_id){
		//地区信息
		$addr = model_area::get_tmp_train_addr($area_id);
		self::smarty() -> assign('addr', $addr);
		//省份
		$province = model_area::get_province_list();
		array_unshift($province,array("province"=>"请选择省会"));
		self::smarty() -> assign('province', $province);
		//城市
		$city = model_area::get_city_list($addr['province']);
		array_unshift($city,array("area_id"=>"", 'city'=>'请选择城市'));
		self::smarty() -> assign('city', $city);
		//地区站
		$area = model_area::get_area_station($addr['area_id']);
		array_unshift($area,array("area_train_id"=>"", 'area_station'=>'请选择地区站'));
		self::smarty() -> assign('area', $area);
	}
	//海运地址处理
	private static function ship_addr($area_id){
		//地区信息
		$addr = model_area::get_tmp_ship_addr($area_id);
		self::smarty() -> assign('addr', $addr);
		//省份
		$province = model_area::get_province_list();
		array_unshift($province,array("province"=>"请选择省会"));
		self::smarty() -> assign('province', $province);
		//城市
		$city = model_area::get_city_list($addr['province']);
		array_unshift($city,array("area_id"=>"", 'city'=>'请选择城市'));
		self::smarty() -> assign('city', $city);
		//港口
		$area = model_area::get_port_list($addr['area_id']);
		array_unshift($area,array("area_ship_id"=>"", 'port'=>'请选择港口'));
		self::smarty() -> assign('area', $area);
		//过驳目的港
		$street = model_area::get_port_dest($area_id);
		array_unshift($street,array("area_ship_id"=>"", 'port_dest'=>'请选择过驳目的港'));
		self::smarty() -> assign('street', $street);
	}
	//水汽运地址处理
	private static function smotor_addr($area_id){
		//地区信息
		$addr = model_area::get_tmp_smotor_addr($area_id);
		self::smarty() -> assign('addr', $addr);
		//省份
		$province = model_area::get_province_list();
		array_unshift($province,array("province"=>"请选择省会"));
		self::smarty() -> assign('province', $province);
		//城市
		$city = model_area::get_city_list($addr['province']);
		array_unshift($city,array("area_id"=>"", 'city'=>'请选择城市'));
		self::smarty() -> assign('city', $city);
		//地区码头
		$area = model_area::get_area_wharf($addr['area_id']);
		array_unshift($area,array("area_smotor_id"=>"", 'area_wharf'=>'请选择地区码头'));
		self::smarty() -> assign('area', $area);
	}*/
	//保存表格数据
	public static function save_table(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$data = self::get_request_data();
		$tmp_id = intval($data['tmp_id']);
		if(empty($tmp_id)){
			self::ajax_ouput(300, '操作失败,模板ID不能为空');
		}
		unset($data['tmp_id']);
		if(empty($data['price'])){
			self::ajax_ouput(300, '操作失败,模板数据不能为空');
		}
		$result = model_transportprice::save_table_data($tmp_id, $data['price']);
		if(!empty($result)){
			self::ajax_ouput(200, '操作成功','transporttmp','closeCurrent');
		}else{
			self::ajax_ouput(300, '操作失败');
		}
	}
}
